﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

/* C# Symmetric key encryption (also known as secret-key encryption), is a cryptographic technique that uses a single secret key to encrypt and decrypt data. 
 * The methods within symmetric encryption are also known as “ciphers”, and process plain text using the secret key to generate encrypted data, called “cipher text”.
 * 
 * Symmetric algorithms are fast and are convenient when encrypting large amounts of data. 
 * Decrypting the data without the key can take an attacker hundreds of years (if a proper key length is used).
 * 
 * The disadvantage of this technique is that the secret key must be available to both the process that encrypts the data and the process that decrypts the data. 
 * This is a challenging problem because the key itself cannot be protected with symmetric encryption.
 * The secure way to exchange the symmetric key is to encrypt it asymmetrically.
 */
namespace VMCCryptoLib.SymmetricEncryption
{
    /// <summary>
    /// RC2 – The key length is variable. This algorithm was designed to replace the DES, using variable key size.
    /// </summary>
    public class RC2Encryption
    {
    	#region --- Methods to encrypt and decrypt a string
    	/// <summary>
    	/// 
    	/// </summary>
    	/// <param name="clearData"></param>
    	/// <param name="password"></param>
    	/// <returns></returns>
    	public static string EncryptString(string clearData, string password)
    	{
    		string rs = string.Empty;
			return rs;   		
    	}
    	
    	/// <summary>
    	/// 
    	/// </summary>
    	/// <param name="cipherData"></param>
    	/// <param name="password"></param>
    	/// <returns></returns>
    	public static string DecryptString(string cipherData, string password)
    	{
    		string rs = string.Empty;
    		return rs;
    	
    	}
    	#endregion
    	
    	#region --- Methods to encrypt and decrypt a file
    	
    	#endregion
    	
    	#region --- Private Methods
    	/// <summary>
    	/// 
    	/// </summary>
    	/// <param name="clearData"></param>
    	/// <param name="Key"></param>
    	/// <param name="IV"></param>
    	/// <returns></returns>
    	public static byte[] RC2Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    	{
    		MemoryStream memoStream = new MemoryStream();
    		RC2 algo = RC2.Create();
    		algo.Key=Key;
    		algo.IV=IV;
    		CryptoStream crpStream = new CryptoStream(memoStream, algo.CreateEncryptor(), CryptoStreamMode.Write);
    		crpStream.Write(clearData,0,clearData.Length);
    		crpStream.Close();
    		byte[] encryptedData = memoStream.ToArray();
    		return encryptedData;
    	}
    	
    	/// <summary>
    	/// 
    	/// </summary>
    	/// <param name="cipherData"></param>
    	/// <param name="Key"></param>
    	/// <param name="IV"></param>
    	/// <returns></returns>
    	public static byte[] RC2Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
    	{
    		MemoryStream memoStream = new MemoryStream();
    		RC2 algo = RC2.Create();
    		algo.Key=Key;
    		algo.IV = IV;
    		CryptoStream crpStream = new CryptoStream(memoStream,algo.CreateDecryptor(), CryptoStreamMode.Write);
    		crpStream.Write(cipherData,0,cipherData.Length);
    		crpStream.Close();
    		byte[] decryptedData = memoStream.ToArray();
    		return decryptedData;
    	}
    	#endregion
    }
}
